package springboot.dao;

import springboot.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private RoleDao roleDao;

    public boolean addUser(User user){
        String sql = "insert into user (username,password,role_id) value (?,?,?)";
        int a = jdbcTemplate.update(sql,user.getUsername(),user.getPassword(),user.getRole().getId());
        if (a != 1 ){
            return false;
        }else{
            return true;
        }
    }
    public boolean updateUserById(int id,User user){
        String sql = "update user set password = ? where id = ?";
        int a = jdbcTemplate.update(sql,user.getPassword(),id);
        if (a != 1 ){
            return false;
        }else{
            return true;
        }
    }
    public boolean deleteUserById(int id){
        String sql = "delete from user where id = ?";
        int a = jdbcTemplate.update(sql,id);
        if (a != 1 ){
            return false;
        }else{
            return true;
        }
    }
    public List<User> getUsers(){
        String sql = "select id,username, password,role_id from user";
        List<User> users = jdbcTemplate.query(sql,new RowMapper<User>(){
            @Override
            public User mapRow(ResultSet rs,int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(roleDao.getRolesById(rs.getInt("role_id")));
                return user;
            }
        });
        return users;
    }
    public User getUserById(int id){
        String sql = "select username ,password from user where id = ?";
        List<User> users = jdbcTemplate.query(sql,new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(id);
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(roleDao.getRolesById(rs.getInt("role_id")));
                return user;
            }
        },id);
        if (users.size()>0){
            return users.get(0);
        }else{
            return null;
        }
    }
    public String getUsernameById(int id){
        String sql = "select username from user where id = ?";
        List<String> names = jdbcTemplate.query(sql,new SingleColumnRowMapper<String>(String.class),id);
        if (names.size()>0){
            return names.get(0);
        }else{
            return null;
        }
    }
    public User getUserByName(String name) {
        String sql = "select id,username ,password ,role_id from user where username = ?";
        List<User> users = jdbcTemplate.query(sql, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(roleDao.getRolesById(rs.getInt("role_id")));
                return user;
            }
        }, name);
        if (users.size() > 0) {
            return users.get(0);
        } else {
            return null;
        }
    }
}
